package com.guichun.select;

import com.guichun.Jdbcs;

import java.sql.*;

/**
 * @author guichun
 * @version 1.0.0
 * @ClassName JdbcSe.java
 * @email onebigbai@icloud.com
 * @Description Jdbc查询
 * @createTime 2021-03-09
 */
public class JdbcSe {
    public JdbcSe(Jdbcs jdbcs) {
        try{
            // 获取连接
            Connection connection = DriverManager.getConnection(jdbcs.getJdbcUrl(), jdbcs.getJdbcRoot(), jdbcs.getJdbcPassword());

            // 报表（防止sql注入）
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT id, name ,gender, grade FROM students WHERE grade = ?");

            // 查询
            preparedStatement.setObject(1,3);

            //返回集
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int grade = resultSet.getInt("grade");

                System.out.println("name = " + name);
                System.out.println("grade = " + grade);
            }
            // 关闭连接
            connection.close();
        }catch (SQLException e) {
            System.out.println("e = " + e);
        }
    }
}
